package com.justalk.testhandlersleepclock;

import androidx.appcompat.app.AppCompatActivity;

import android.nfc.Tag;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private long mStartTimestamp;
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void myOnclick(View target){
        switch (target.getId()) {
            case R.id.bt_test_sleep:
                mStartTimestamp = SystemClock.elapsedRealtime();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this,"弹出延时2s的吐司",Toast.LENGTH_SHORT).show();
                        long nowSpeedTime= SystemClock.elapsedRealtime() - mStartTimestamp;
                        Log.d(TAG,"执行共等了:"+nowSpeedTime+"ms");
                    }
                },2000);
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                break;
        }
    }
}
